package com.ruoyi.wlxz.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wlxz.domain.WxzPublishedActivity;
import com.ruoyi.wlxz.domain.WxzSupplementRecord;
import com.ruoyi.wlxz.mapper.WxzPublishedActivityMapper;
import com.ruoyi.wlxz.service.IWxzPublishedActivityService;
import com.ruoyi.wlxz.util.DataCronUtil;
import com.ruoyi.wlxz.util.QuartzSchedulerActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 活动发布Service业务层处理
 *
 * @author 张浩
 * @date 2025-07-21
 */
@Service
public class WxzPublishedActivityServiceImpl implements IWxzPublishedActivityService {
    @Autowired
    private WxzPublishedActivityMapper wxzPublishedActivityMapper;
    @Autowired
    private DataCronUtil dataCronUtil;
    @Autowired
    private QuartzSchedulerActivity quartzSchedulerActivity;
    /**
     * 查询活动发布
     *
     * @param activityId 活动发布主键
     * @return 活动发布
     */
    @Override
    public WxzPublishedActivity selectWxzPublishedActivityByActivityId(Long activityId) {
        return wxzPublishedActivityMapper.selectWxzPublishedActivityByActivityId(activityId);
    }
    /**
     * 查询活动发布列表
     *
     * @param wxzPublishedActivity 活动发布
     * @return 活动发布
     */
    @Override
    public List<WxzPublishedActivity> selectWxzPublishedActivityList(WxzPublishedActivity wxzPublishedActivity) {
        return wxzPublishedActivityMapper.selectWxzPublishedActivityList(wxzPublishedActivity);
    }
    /**
     * 新增活动发布
     *
     * @param wxzPublishedActivity 活动发布
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWxzPublishedActivity(WxzPublishedActivity wxzPublishedActivity) {
        //活动开始时间
        Date ctivityStart = wxzPublishedActivity.getActivityStart();
        String ActivityName = wxzPublishedActivity.getActivityName();
        String ctivityStartcron = dataCronUtil.getCron(ctivityStart);
        if (StringUtils.isNotEmpty(ctivityStartcron)){
            quartzSchedulerActivity.ongoing(ctivityStartcron,ActivityName);
        }
        //活动结束时间
        Date ctivityEnd = wxzPublishedActivity.getActivityEnd();
        String ctivityEndcron = dataCronUtil.getCron(ctivityEnd);
        if (StringUtils.isNotEmpty(ctivityEndcron)){
            quartzSchedulerActivity.end(ctivityEndcron,ActivityName);
        }
        //开始报名时间
        Date ctivityRegistrationStart = wxzPublishedActivity.getRegistrationStart();
        String ctivityRegistrationStartcron = dataCronUtil.getCron(ctivityRegistrationStart);
        if (StringUtils.isNotEmpty(ctivityRegistrationStartcron)){
            quartzSchedulerActivity.start(ctivityRegistrationStartcron,ActivityName);
        }
        //结束报名时间
        Date ctivityRegistrationEnd = wxzPublishedActivity.getRegistrationEnd();
        String ctivityRegistrationEndcron = dataCronUtil.getCron(ctivityRegistrationEnd);
        if (StringUtils.isNotEmpty(ctivityRegistrationEndcron)){
            quartzSchedulerActivity.wait(ctivityRegistrationEndcron,ActivityName);
        }
        //新增活动
        wxzPublishedActivity.setCreateTime(DateUtils.getNowDate());
        int rows = wxzPublishedActivityMapper.insertWxzPublishedActivity(wxzPublishedActivity);
        //新增导出表格
        insertWxzSupplementRecord(wxzPublishedActivity);
        return rows;
    }
    /**
     * 修改活动发布
     *
     * @param wxzPublishedActivity 活动发布
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWxzPublishedActivity(WxzPublishedActivity wxzPublishedActivity) {
        wxzPublishedActivityMapper.deleteWxzSupplementRecordByActivityId(wxzPublishedActivity.getActivityId());
        insertWxzSupplementRecord(wxzPublishedActivity);
        return wxzPublishedActivityMapper.updateWxzPublishedActivity(wxzPublishedActivity);
    }
    /**
     * 批量删除活动发布
     *
     * @param activityIds 需要删除的活动发布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxzPublishedActivityByActivityIds(Long[] activityIds) {
        wxzPublishedActivityMapper.deleteWxzSupplementRecordByActivityIds(activityIds);
        return wxzPublishedActivityMapper.deleteWxzPublishedActivityByActivityIds(activityIds);
    }
    /**
     * 删除活动发布信息
     *
     * @param activityId 活动发布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxzPublishedActivityByActivityId(Long activityId) {
        wxzPublishedActivityMapper.deleteWxzSupplementRecordByActivityId(activityId);
        return wxzPublishedActivityMapper.deleteWxzPublishedActivityByActivityId(activityId);
    }
    /**
     * 新增学分补录信息
     *
     * @param wxzPublishedActivity 活动发布对象
     */
    public void insertWxzSupplementRecord(WxzPublishedActivity wxzPublishedActivity) {
        List<WxzSupplementRecord> wxzSupplementRecordList = wxzPublishedActivity.getWxzSupplementRecordList();
        Long activityId = wxzPublishedActivity.getActivityId();
        if (StringUtils.isNotNull(wxzSupplementRecordList)) {
            List<WxzSupplementRecord> list = new ArrayList<WxzSupplementRecord>();
            for (WxzSupplementRecord wxzSupplementRecord : wxzSupplementRecordList) {
                wxzSupplementRecord.setActivityId(activityId);
                list.add(wxzSupplementRecord);
            }
            if (list.size() > 0) {
                wxzPublishedActivityMapper.batchWxzSupplementRecord(list);
            }
        }
    }
}
